package com.zw.pdm.module.afterSale.controller;


import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.module.afterSale.Service.CheckService;
import com.zw.pdm.module.afterSale.model.CheckModel;
import com.zw.pdm.module.afterSale.model.FileModel;
import com.zw.pdm.module.afterSale.model.ProjectModel;
import com.zw.pdm.module.basicSale.model.DictEnumModel;
import com.zw.pdm.module.basicSale.service.DictEnumService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.common.JsonResult.ResultType.ERROR;
import static com.zw.pdm.common.JsonResult.ResultType.SUCCESS;


@Controller
public class CheckController extends BaseController {

    @Autowired
    private CheckService checkService;
    @Autowired
    private DictEnumService dictEnumService;


    /**
     * @Description: 首页跳转到待发起验收列表页面
     * @Author: zcb
     * @Date: 2020-12-10 08:28:01
     * @Param:[model]
     * @Return:java.lang.String
     **/
    @GetMapping("/unCheck/list")
    @RequiresPermissions({"aft:check:view"})
    public String unCheckPage(Model model) {
        DictEnumModel dict1 = new DictEnumModel();
        dict1.setGroupName("验收负责人");
        List<DictEnumModel> dictLists1 = dictEnumService.selectDictEnumByConditon(dict1);
        model.addAttribute("dictLists1", dictLists1);
        return "/afterSale/unCheckList";
    }

    /**
     * @Description: listtechManagePage 跳转技术资料管理页面
     * @Author: zcb
     * @Date: 2021-01-06 16:22:39
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/pro/techManage")
    @RequiresPermissions({"aft:agree:view"})
    public String listTechManagePage(Model model) {
        return "/afterSale/techManage";
    }

    /**
     * @Description: 首页跳转到发起验收列表页面
     * @Author: zcb
     * @Date: 2020-12-24 16:46:56
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/list")
    @RequiresPermissions({"aft:initiate:view"})
    public String listPage(Model model) {
        return "/afterSale/checkList";
    }


    /**
     * @Description: 跳转到验收单编辑页面
     * @Author: zcb
     * @Date: 2020-12-10 08:37:44
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/edit")
    @RequiresPermissions({"aft:initiate:edit"})
    public String checkEditPage(@RequestParam("proId") Integer proId,
                                @RequestParam("conCode") String conCode,
                                @RequestParam("conStatus") String conStatus,
                                Model model) {
        int checkDetailId = checkService.selcetProInfoByProId(proId).getCheckDetailId();
        model.addAttribute("conCode", conCode);
        model.addAttribute("conStatus", conStatus);
        model.addAttribute("proId", proId);
        /** 初次创建 跳转到 checkAdd */
        if (checkDetailId == 0) {
            //技术资料下拉框字典表
            DictEnumModel dict1 = new DictEnumModel();
            dict1.setGroupName("验收附件类型");
            List<DictEnumModel> dictLists1 = dictEnumService.selectDictEnumByConditon(dict1);
            model.addAttribute("dictLists1", dictLists1);
            //验收类别下拉框字典表
            DictEnumModel dict2 = new DictEnumModel();
            dict2.setGroupName("验收类型");
            List<DictEnumModel> dictLists2 = dictEnumService.selectDictEnumByConditon(dict2);
            model.addAttribute("dictLists2", dictLists2);
            //验收单据  基本项目信息
            ProjectModel proInfo = checkService.selcetProInfoByProId(proId);
            model.addAttribute("proInfo", proInfo);

            CheckModel checkModel2 = checkService.selectBoHuiInfoByProId(proId);
            model.addAttribute("checkModel2", checkModel2);
            return "/afterSale/checkAdd";
        } else {
            /** 修改  跳转到 checkEdit*/
            //技术资料下拉框字典表
            DictEnumModel dict1 = new DictEnumModel();
            dict1.setGroupName("验收附件类型");
            List<DictEnumModel> dictLists1 = dictEnumService.selectDictEnumByConditon(dict1);
            model.addAttribute("dictLists1", dictLists1);
            //验收类别下拉框字典表
            DictEnumModel dict2 = new DictEnumModel();
            dict2.setGroupName("验收类型");
            List<DictEnumModel> dictLists2 = dictEnumService.selectDictEnumByConditon(dict2);
            model.addAttribute("dictLists2", dictLists2);
            //验收明细表 基本信息
            CheckModel checkModel = checkService.selectCheckDetailByProId(proId);
            model.addAttribute("checkModel", checkModel);
            //技术资料表 信息
            List<FileModel> fileModelList = checkService.selectTechByDetailId(checkModel.getCheckDetailId());
            model.addAttribute("fileModelList", fileModelList);

            CheckModel checkModel2 = checkService.selectBoHuiInfoByProId(proId);
            model.addAttribute("checkModel2", checkModel2);
            return "/afterSale/checkEdit";
        }

    }


    /**
     * 查找验收附件类型列表
     *
     * @param
     * @return
     */
    @PostMapping("/check/annexItem")
    @ResponseBody
    public List<DictEnumModel> findAnnexItem() {
        DictEnumModel dict = new DictEnumModel();
        dict.setGroupName("验收附件类型");
        return dictEnumService.selectDictEnumByConditon(dict);
    }


    /**
     * @Description: checkSelectPage跳转至验收单查看页面
     * @Author: zcb
     * @Date: 2020-12-16 12:45:58
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/select")
    //@RequiresPermissions({"basic:dept:view"})
    public String checkSelectPage(@RequestParam("proId") Integer proId,
                                  @RequestParam("conCode") String conCode,
                                  @RequestParam("conStatus") String conStatus,
                                  Model model) {
        if (conCode==null||conCode.equals("")||conCode.equals("null")){
            model.addAttribute("conCode", "");
        }else{
            model.addAttribute("conCode", conCode);

        }
        model.addAttribute("conStatus", conStatus);
        model.addAttribute("proId", proId);
        CheckModel checkModel = checkService.selectCheckDetailByProId(proId);
        model.addAttribute("checkModel", checkModel);
        List<FileModel> fileModelList = checkService.selectTechByDetailId(checkModel.getCheckDetailId());
        model.addAttribute("fileModelList", fileModelList);
        CheckModel checkModel2 = checkService.selectBoHuiInfoByProId(proId);
        model.addAttribute("checkModel2", checkModel2);
        return "/afterSale/checkSelect";
    }

    /**
     * @Description: checkExaminePage 跳转至验收单审核页面
     * @Author: zcb
     * @Date: 2021-01-05 15:43:35
     * @Param: [proId, model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/examine")
    @RequiresPermissions({"aft:alter:check"})
    public String checkExaminePage(@RequestParam("proId") Integer proId,
                                   @RequestParam("conCode") String conCode,
                                   @RequestParam("conStatus") String conStatus,
                                   Model model) {
        model.addAttribute("conCode", conCode);
        model.addAttribute("conStatus", conStatus);
        model.addAttribute("proId", proId);
        CheckModel checkModel = checkService.selectCheckDetailByProId(proId);
        model.addAttribute("checkModel", checkModel);
        List<FileModel> fileModelList = checkService.selectTechByDetailId(checkModel.getCheckDetailId());
        model.addAttribute("fileModelList", fileModelList);
//        List<FileModel> fileInfoModelList =checkService.selectFileInfoByDetailId(checkModel.getCheckDetailId());
//        model.addAttribute("fileInfoModelList",fileInfoModelList);
        return "/afterSale/checkExamine";
    }

    /**
     * @Description: selectFileList 查询验收单附件列表
     * @Author: zcb
     * @Date: 2021-01-23 09:14:18
     * @Param: [checkDetailId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/selectFileList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectFileList(@RequestParam("checkDetailId") Integer checkDetailId) {
        return jsonResult(checkService.selectFileInfoByDetailId(checkDetailId,1));
    }


    /**
     * @Description: selectTechFileList 查询项目 技术协议附件列表
     * @Author: zcb
     * @Date: 2021-01-23 09:14:32
     * @Param: [proId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/selectTechFileList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectTechFileList(@RequestParam("proId") Integer proId) {
        return jsonResult(checkService.selectFileInfoByDetailId(proId,2));
    }

    /**
     * @Description: checkExaminePage跳转至验收单审核页面
     * @Author: zcb
     * @Date: 2020-12-16 15:20:53
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/examineList")
    @RequiresPermissions({"aft:alter:view"})
    public String checkExaminePage(Model model) {
        return "/afterSale/checkExamineList";
    }

    /**
     * @Description: checkSelectPage 跳转到验收单查询页面
     * @Author: zcb
     * @Date: 2021-01-23 10:11:15
     * @Param: [model]
     * @Return: java.lang.String
     **/
    @GetMapping("/check/selectListPage")
    @RequiresPermissions({"aft:certificate:view"})
    public String checkSelectPage(Model model) {
        return "/afterSale/checkListSelect";
    }


    /**
     * @Description: uploadExtends 上传附件到临时文件夹
     * @Author: zcb
     * @Date: 2020-12-26 08:16:23
     * @Param: [file]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/tempUpload")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult uploadExtends(@RequestParam("file") MultipartFile file) {
        String path = checkService.uploadTemp(file);
        return success().setData(path);
    }

    /**
     * @Description: selectUnCheckList 查询待发起验收列表
     * @Author: zcb
     * @Date: 2020-12-26 08:20:16
     * @Param: []
     * @Return: void
     **/
    @ResponseBody
    @PostMapping("check/selectUnCheckList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectUnCheckList(ProjectModel projectModel) {
//        startPage();
        return jsonResult(checkService.selectUnCheckList(projectModel));
    }


    /**
     * @Description: selectCheckList 查询已发起验收列表
     * @Author: zcb
     * @Date: 2020-12-26 16:16:38
     * @Param: []
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/selectCheckList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectCheckList(ProjectModel projectModel) {
        startPage();
        return jsonResult(checkService.selectCheckList(projectModel));
    }

    /**
     * @Description: selectChecExaminekList 查询待审核验收列表
     * @Author: zcb
     * @Date: 2021-01-05 15:07:37
     * @Param: [projectModel]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/selectCheckExamineList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectChecExamineList(ProjectModel projectModel) {
        startPage();
        return jsonResult(checkService.selectCheckExamineList(projectModel));
    }

    /**
     * @Description: queryList验收单查询页面
     * @Author: zcb
     * @Date: 2021-01-05 20:05:46
     * @Param: [projectModel]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/queryList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult queryList(ProjectModel projectModel) {
        startPage();
        return jsonResult(checkService.queryList(projectModel));
    }

    /**
     * @Description: lunch 项目发起验收
     * @Author: zcb
     * @Date: 2020-12-26 13:06:21
     * @Param: [proId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/addNewCheck")
    @RequiresPermissions({"aft:checkInitiate:view"})
    public JsonResult lunch(Integer proId,String director, Model model) {
        JsonResult jsonResult = new JsonResult();
//        User user = checkService.getUserByName(director);
        int i = checkService.insertNewCheck(proId,director);
        if (i == 1) {
            jsonResult.setMsg("发起成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("发起失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }

    /**
     * @Description: editSave 验收单保存
     * @Author: zcb
     * @Date: 2020-12-28 10:35:42
     * @Param: []
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/editSave")
    public JsonResult editSave(CheckModel checkModel,
                               int[] annexType, String[] fileName, String[] relevantDepartment, String[] finishDate, String[] uploadRemark,
                               String[] tempUrl2, String[] fileName2, String[] uploader2, String[] uploadTime2, String[] uploadRemark2
    ) {
        JsonResult jsonResult = new JsonResult();
        String res = checkService.editSave(checkModel,
                annexType, fileName, relevantDepartment, finishDate, uploadRemark,
                tempUrl2, fileName2, uploader2, uploadTime2, uploadRemark2);
        if (res.equals("保存成功")) {
            jsonResult.setMsg("保存成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("保存失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }

    /***
     * @Description: uploadFile 文件上传
     * @Author: zcb
     * @Date: 2020-12-30 18:13:31
     * @Param: [file, checkDetailId, remark]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("/check/uploadFile")
    @RequiresPermissions({"aft:agree:upload"})
    public JsonResult uploadFile(MultipartFile[] file, Integer proId, String remark, int fileType
    ) {
        JsonResult jsonResult = new JsonResult();

        int i = checkService.uploadFile(file, proId, remark, fileType);
        int j= checkService.updateProTechStatus(proId);
        if (i == 1&&j==1) {
            jsonResult.setType(SUCCESS);
            jsonResult.setMsg("上传成功");
            return jsonResult;
        } else {
            jsonResult.setMsg("上传失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }


    /**
     * @Description: techSelect 技术说明查询
     * @Author: zcb
     * @Date: 2020-12-30 18:13:43
     * @Param: [checkDetailId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @GetMapping("check/techSelect")
    public JsonResult techSelect(Integer checkDetailId) {
        JsonResult jsonResult = new JsonResult();
        List<FileModel> fileList = checkService.selectTechByDetailId(checkDetailId);
        jsonResult.setData(fileList);
        jsonResult.setType(SUCCESS);
        return jsonResult;
    }


    /***
     * @Description: checkDelFile 删除附件
     * @Author: zcb
     * @Date: 2021-01-04 15:45:14
     * @Param: [fileId]
     * @Return: java.lang.String
     **/
    @ResponseBody
    @PostMapping("/checkDelFile")
    @RequiresPermissions({"aft:agree:del"})
    public String checkDelFile(Integer fileId) {
        String result = checkService.delFileById(fileId);
        // todo 查询该项目附件数是否为0  对应修改是否上传技术协议状态值
        int count = checkService.queryCount(fileId,1);
        int proId = checkService.queryProId(fileId);
        if (count==0){
           checkService.updateTechStatus(proId);
        }
        return result;
    }


    /**
     * @Description: submitCheck 验收单提交
     * @Author: zcb
     * @Date: 2021-01-05 13:39:57
     * @Param: [proId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/submit")
    @RequiresPermissions({"aft:initiate:commit"})
    public JsonResult submitCheck(Integer proId) {
        JsonResult jsonResult = new JsonResult();
        int i = checkService.submitCheck(proId);
        if (i == 1) {
            jsonResult.setMsg("提交成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("提交失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }

    /**
     * @Description: ExamineCheckYes 审核通过
     * @Author: zcb
     * @Date: 2021-01-05 18:14:04
     * @Param: [checkId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/examineYes")
    public JsonResult ExamineCheckYes(Integer checkId, String eqAuditOpinoin) {
        JsonResult jsonResult = new JsonResult();
        int i = 0;
        i = checkService.ExamineCheck(checkId, eqAuditOpinoin, 4);
        if (i == 1) {
            jsonResult.setMsg("审核成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("审核失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }

    /**
     * @Description: ExamineCheckNo 审核驳回
     * @Author: zcb
     * @Date: 2021-01-05 18:14:12
     * @Param: [checkId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/examineNo")
    public JsonResult ExamineCheckNo(Integer checkId, String eqAuditOpinoin) {
        JsonResult jsonResult = new JsonResult();
        int i = checkService.ExamineCheck(checkId, eqAuditOpinoin, 2);
        if (i == 1) {
            jsonResult.setMsg("驳回成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("驳回失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }

    /**
     * @Description: selectTechList 查询技术资料管理页列表
     * @Author: zcb
     * @Date: 2021-01-06 20:37:03
     * @Param: [projectModel]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("check/selectTechList")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectTechList(ProjectModel projectModel) {
        startPage();
        return jsonResult(checkService.selectTechList(projectModel));
    }


//    @ResponseBody
//    @PostMapping("updateProTechStatus")
////    @RequiresPermissions({"pay:infor:upload"})
//    public JsonResult updateProTechStatus(int proId) {
//        JsonResult jsonResult = new JsonResult();
//        int i = checkService.updateProTechStatus(proId);
//        if (i == 1) {
//            jsonResult.setType(SUCCESS);
//            return jsonResult;
//        } else {
//            jsonResult.setType(ERROR);
//            return jsonResult;
//        }
//    }

    /**
     * @Description: selectAnnexList 技术协议附件列表
     * @Author: zcb
     * @Date: 2021-01-07 08:42:43
     * @Param: [proId]
     * @Return: com.zhongwang.sale.common.JsonResult
     **/
    @ResponseBody
    @PostMapping("annexListByProId")
//    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult selectAnnexList(int proId) {
        //startPage();
        return jsonResult(checkService.selectAnnexList(proId));
    }


    @ResponseBody
    @PostMapping("techIsNeed")
    @RequiresPermissions({"aft:agree:edit"})
    public JsonResult techIsNeed(int proId,int proTechAgreement) {
        JsonResult jsonResult = new JsonResult();
        int s =  checkService.techIsNeedSave(proId,proTechAgreement);
        if (s==1) {
            jsonResult.setMsg("保存成功");
            jsonResult.setType(SUCCESS);
            return jsonResult;
        } else {
            jsonResult.setMsg("保存失败，请联系管理员");
            jsonResult.setType(ERROR);
            return jsonResult;
        }
    }



}
